import utils.FileUtils;
import utils.SystemLookup;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author PC.
 */
public class HashTable {
    private final static int[] RANDOM_CODES = {
            0x5c, 0x3b, 0x02, 0xda, 0xe8, 0x9c, 0xd1, 0x71, 0xfa, 0x52, 0xd6,
            0xd2, 0x89, 0x0c, 0x70, 0x77, 0xdc, 0x04, 0xae, 0x10, 0xc7, 0x45,
            0x62, 0xd9, 0x07, 0x27, 0x9d, 0x88, 0x38, 0x4e, 0xbe, 0xe9, 0x42,
            0xa0, 0x6b, 0xcc, 0x22, 0x50, 0x64, 0xfc, 0x3d, 0xbb, 0xdb, 0xb2,
            0x36, 0x53, 0xcb, 0x93, 0x39, 0x6c, 0x43, 0xfe, 0x3e, 0x57, 0x40,
            0xb3, 0x54, 0x80, 0x2b, 0x2e, 0x99, 0xa4, 0xc1, 0x5b, 0x41, 0x5d,
            0x9f, 0x7d, 0xf6, 0xe5, 0x21, 0x5f, 0xf4, 0xd7, 0xf8, 0x06, 0x1b,
            0xc0, 0xc9, 0x1e, 0xc8, 0x11, 0x13, 0x18, 0x55, 0x28, 0x49, 0xf3,
            0x79, 0x0f, 0xad, 0x1f, 0x83, 0xcd, 0x97, 0xb7, 0x2d, 0x82, 0x7b,
            0xa8, 0xf7, 0xb0, 0x37, 0xc5, 0x23, 0xe0, 0x68, 0xc6, 0x16, 0xe4,
            0x31, 0x8b, 0x9e, 0x17, 0x1a, 0x15, 0x6a, 0x0a, 0x78, 0xdd, 0xe7,
            0xaf, 0xec, 0xf9, 0x58, 0x2a, 0x8a, 0xd0, 0x14, 0xfb, 0x20, 0x9a,
            0x12, 0xca, 0x24, 0x75, 0xb1, 0x3f, 0xf0, 0x5e, 0xea, 0xed, 0x73,
            0xd4, 0xde, 0x4f, 0x3c, 0x60, 0xc4, 0xb4, 0x6d, 0x61, 0x32, 0x48,
            0x01, 0xf5, 0x2f, 0x7a, 0xe3, 0x7e, 0xbf, 0x29, 0xd5, 0xab, 0x35,
            0x98, 0x5a, 0x26, 0x47, 0x69, 0x4a, 0x30, 0x8d, 0x1c, 0x95, 0x08,
            0xa7, 0xf2, 0x4d, 0xc2, 0x9b, 0xa1, 0x6f, 0xb6, 0x84, 0xac, 0x8c,
            0x66, 0x19, 0x56, 0xc3, 0x72, 0x46, 0x65, 0x00, 0x0e, 0x8f, 0xe2,
            0x92, 0x2c, 0x4c, 0xa2, 0x63, 0x1d, 0xa9, 0xcf, 0x6e, 0x8e, 0xb5,
            0xdf, 0xbc, 0x4b, 0x25, 0x3a, 0x33, 0x91, 0x7f, 0x05, 0xba, 0xaa,
            0xeb, 0xee, 0x44, 0xe1, 0x0d, 0x03, 0x86, 0x09, 0xb9, 0x76, 0xe6,
            0x96, 0xd8, 0x74, 0x7c, 0xce, 0x94, 0xa3, 0xff, 0xfd, 0xbd, 0x67,
            0x85, 0xa5, 0x51, 0xef, 0x90, 0xb8, 0x59, 0x0b, 0x34, 0x81, 0x87,
            0xf1, 0xa6, 0xd3};

//    private final static long MOD = 4294901753L;
//    private final static long BASE = 733763877L;
    private final static int MOD = 2147483647;
    private final static int BASE = 733763877;
    private static int counter = 0;

    public static void main(String[] args) {
        String[] hashTable = new String[65536];
        List<String> wordsList = FileUtils.readTextFile(SystemLookup.PATH_TO_LOAD + "linux.words");
//        List<String> wordsList = Arrays.asList("Zulu", "Zulus", "Zurich");
        Set<Integer> setInt = new HashSet<>();

        for (String s : wordsList) {
//            int hash = xorHash(s);
//            int hash = improvedXorHash(s);
            int hash = modHash(s);
//            int hash = hashFunction(s);
//            int hash = powStringMod(bases[10], modulos[10], s);
            setInt.add(hash);
            //System.out.println(hash);
            if (hashTable[hash] == null) {
                hashTable[hash] = s;
            } else {
                counter++;
                System.out.println("Powtarza się: " + hashTable[hash] + "::" + s);
            }
        }

        System.out.println("Liczba elementow do hashowania: " + wordsList.size());
        System.out.println("Liczba powtórzeń: " + counter);
        System.out.println("Size set: " + setInt.size());

//        System.out.println(Integer.toBinaryString("aa".charAt(1)));
//        System.out.println(Integer.toBinaryString("aa".codePointAt(1)));
//        System.out.println(Integer.toBinaryString("a".hashCode()));
//        System.out.println(Integer.toBinaryString(1));
//        System.out.println(Integer.toBinaryString(1 << 34));
//        System.out.println(Integer.toBinaryString(1 << 6));
//        int hash = powStringMod(bases[10], modulos[10], "Zack");
//        System.out.println("START");
    }

    private static int[] modulos = {65437, 65267, 64811, 64109, 62549, 62299,
            61861, 58771, 58027, 54787, 52901, 52163, 51517, 47123, 44917, 42701};
    private static  int[] bases = {25521, 42805, 25310, 35628, 16795, 31314,
            26025, 48891, 17267, 26506, 529, 8825, 7024, 8530, 29443, 12715};

    private static int powStringMod(int b, int m, String s) {
        int result = 1;
        int mul = b;
        for (int i = 0; i < s.length(); ++i) {
            for (int t = 0; t < 8; ++t) {
                if ((s.codePointAt(i) & (1 << t)) != 0) {
                    result = (result * mul) % m;
                }
                mul = (mul * mul) % m;
            }
        }
        return (result<<16)>>>16;
    }

    private static int modHash(String s) {
        long result = 1;
        long mul = BASE;
        for (int i = 0; i < s.length(); ++i) {
            for (int t = 0; t < 8; ++t) {
                if ((s.charAt(i) & (1 << t)) != 0) {
                    result = ((result * mul) % MOD);
                }
                mul = (mul * mul) % MOD;
            }
        }
//        System.out.println(result+";"+Integer.toBinaryString((int) result)+"??"+Integer.toBinaryString((result<<16)>>>16));
//        return (int) ((result<<16)>>>16);
//        System.out.println(Integer.toBinaryString((int) result));
//        System.out.println(Integer.toBinaryString((int) (result & 0x0000FFFF)));
        return (int) (result & 0x0000FFFF);
//        return (int) (result & 0xFFFF0000) >>16;
    }

    private static boolean parity(int x) {
        int t = x;
        for (int i = 4; i==0 ; --i) {//TODO Warning: Condition 'i==0' is always 'false'
            t ^= (t >> (1 << i));
        }

        return (t & 1) != 0;
    }

    private static int hashFunction(String s) {
        int result = 0;
        int[] multipliers = new int[16];
        int[] powResults = new int[16];
        for (int k = 0; k < 16; ++k) {
            multipliers[k] = bases[k];
            powResults[k] = 1;
        }
        for (int i = 0;
             i < s.length(); ++i) {
            for (int t = 0; t < 8; ++t) {
                if ((s.charAt(i) & (1 << t)) != 0) {
                    for (int k = 0; k < 16; ++k) {
                        powResults[k] = (powResults[k] * multipliers[k]) % modulos[k];
                    }
                }
                for (int k = 0; k < 16; ++k) {
                    multipliers[k] = (multipliers[k] * multipliers[k]) % modulos[k];
                }
            }
        }
        for (int k = 0; k < 16; ++k) {
            if (parity(powResults[k])) {
                result |= (1 << k);
            }
        }
        return result;
    }

    private static int improvedXorHash(String s) {
        int result = 0;
        for (int i = 0; i < s.length(); ++i) {
            result ^= (RANDOM_CODES[s.charAt(i)] << ((i & 1) << 3));
        }
        return result;
    }

    private static int xorHash(String s) {
        int result = 0;
        for (int i = 0; i < s.length(); ++i) {
            result ^= (s.charAt(i) << ((i & 1) << 3));
        }
        return result;
    }
}
